package com.tcreditdemo;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by xiongzheng on 2018/10/11.
 */

public class MeDetailAdapter extends RecyclerView.Adapter {

    private static int TITLE_ITEM=0;
    private static int CONTENT_ITEM=1;

    ArrayList<Integer> datas;

    public boolean switchstate;

    String[] content1=new String[]{"展示详情","用户ID","企业登陆"};

    public MeDetailAdapter(ArrayList<Integer> datas) {
        this.datas = datas;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TITLE_ITEM;
        }else {
            return CONTENT_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType==TITLE_ITEM){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.me_detail_title_view, parent, false);
            return new Titleitem(view);
        }else if (viewType==CONTENT_ITEM){
            view=LayoutInflater.from(parent.getContext()).inflate(R.layout.me_detail_content_view,parent,false);
            return new ContentItem(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // LogUtil.e("--当前位置77--"+position);
        if (holder instanceof Titleitem){
            Titleitem titleitem = (Titleitem) holder;
            if (datas.size()==4){
            titleitem.tv.setText("设置");
            }

            if (datas.size()==2){
                titleitem.tv.setText("商务洽谈");
            }

            if (datas.get(position)==6){
                titleitem.tv.setText("关于");
            }


        }else if (holder instanceof ContentItem){
            ContentItem contentItem = (ContentItem) holder;
            if (datas.size()==2) {
                //contentItem.tv.setText("Email");
                contentItem.st.setLeftTitle("Email");
                contentItem.st.setRightTitle("xxx@tcredit.com");
            }else if (datas.size()==4){
               // contentItem.tv.setText(content1[position-1]);
                contentItem.st.setLeftTitle(content1[position-1]);
                //选择开关
                if (position==1){
                    contentItem.st.setRightItemOnlyswitch();
                    SwitchCompat swtichCompat = contentItem.st.getSwtichCompat();
                    swtichCompat.setChecked(true);
                    swtichCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            OnSwitchCompatChangerListener.getstate(isChecked);
                        }
                    });
                }
                if (position==2){
                    contentItem.st.setRightTitle("id1234567");
                }
                if (position==3){
                    contentItem.st.setRightTitle("未登录");
                }
            }

            if (datas.get(position)==6){
               // contentItem.tv.setText("版本号");
                contentItem.st.setLeftTitle("版本号");
                contentItem.st.setRightTitle("1.0.0");
            }
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }



    class Titleitem extends RecyclerView.ViewHolder{
        public TextView tv;

        public Titleitem(View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.tv);
        }
    }


    class ContentItem extends RecyclerView.ViewHolder{
//        public TextView tv;
//        public ImageView im;
        public SettingItemView st;


        public ContentItem(View itemView) {
            super(itemView);
//            tv=itemView.findViewById(R.id.tv);
//            im=itemView.findViewById(R.id.im);
//            if (onItemClickListener != null) {
//                itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        onItemClickListener.onItemClick(v, getLayoutPosition(), tv.getText().toString());
//                    }
//                });
//            }

            st=itemView.findViewById(R.id.setting_view);
            if (onItemClickListener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(v, getLayoutPosition(), st.getLeftTitle());
                    }
                });
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position,String title);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public interface OnSwitchCompatChangerListener{
        void getstate(boolean state);
    }

    public static OnSwitchCompatChangerListener OnSwitchCompatChangerListener;

    public static void setOnSwitchCompatChangerListener(OnSwitchCompatChangerListener OnSwitchCompatChangerListener){
        MeDetailAdapter.OnSwitchCompatChangerListener=OnSwitchCompatChangerListener;
    }
}
